package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.entityVo.HarmlessReportVo;
import com.antdesignbackend.backend.service.HarmlessReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/harmlessReportPending")
public class HarmlessReportPendingController extends BaseController {
    @Autowired
    HarmlessReportService harmlessReportService;

    private Integer menuId;

    @GetMapping("/index")
    @AuthToken
    public String ProductionIndex(String menuId) {
        this.menuId = Integer.parseInt(menuId);
        return "harmless/harmlessReportPending";
    }

    /**
     * 分页查询
     * @param harmlessReportVo
     * @return
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody HarmlessReportVo harmlessReportVo, HttpServletRequest request) {
        ReturnVO result = new ReturnVO();
        List<HarmlessReportVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<HarmlessReportVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",2).eq("harmlessId",getLoginEnterpriseId());
            if (harmlessReportVo.getEnterpriseName() != null && !harmlessReportVo.getEnterpriseName().equals(""))
                queryWrapper.like("enterpriseName",harmlessReportVo.getEnterpriseName());
            if (harmlessReportVo.getTimeInterval() != null && !harmlessReportVo.getTimeInterval().equals("")){
                String[] str=harmlessReportVo.getTimeInterval().split(" - ");
                String star=str[0];
                String end=str[1];
                queryWrapper.between("createTime",star,end);
            }
            queryWrapper.orderByDesc("createTime");
            int total=harmlessReportService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= harmlessReportService.search(queryWrapper,new Page<>(harmlessReportVo.getPageIndex(),harmlessReportVo.getPageSize()));
                //得到当前菜单具备的功能权限
                //获得当前角色
//                Object roleID = request.getSession().getAttribute("RoleID");
//                List<Button> list = permissionService.filterPermission(String.valueOf(roleID), menuId);
//                datalist.forEach(model -> model.setButtonList(list));
            }
            result.setData(datalist);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }
    /**
     * 分页查询
     * @param
     * @return
     */
    @PostMapping("/tableSearchMe/{eid}")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@PathVariable("eid") int eid) {
        ReturnVO result = new ReturnVO();
        List<HarmlessReportVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<HarmlessReportVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",2).eq("harmlessId",eid);
            queryWrapper.orderByDesc("createTime");
            int total=harmlessReportService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= harmlessReportService.search(queryWrapper,new Page<>(1,10));
            }
            result.setData(datalist);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }
}
